<template>
  <div @mouseleave="leaveShow" @mouseenter="enterShow">
    <h2 class="all">全部商品分类</h2>
    <!-- 过渡动画 -->
    <transition name="sort">
      <div class="sort" v-show="show">
        <div class="all-sort-list2" @click="goSearch">
          <div
            class="item"
            v-for="(c1, index) in categoryList"
            :key="c1.categoryId"
          >
            <h3
              @mouseenter="enterIndex(index)"
              :class="{ 'item-hover': index == currentIndex }"
            >
              <a :data-Name="c1.categoryName" :data-level1="c1.categoryId">{{
                c1.categoryName
              }}</a>
            </h3>

            <div
              class="item-list clearfix"
              :style="{ display: currentIndex == index ? 'block' : 'none' }"
            >
              <div
                class="subitem"
                v-for="c2 in c1.categoryChild"
                :key="c2.categoryId"
              >
                <dl class="fore">
                  <dt>
                    <a
                      :data-Name="c2.categoryName"
                      :data-level2="c2.categoryId"
                      >{{ c2.categoryName }}</a
                    >
                  </dt>
                  <dd>
                    <em v-for="c3 in c2.categoryChild" :key="c3.categoryId">
                      <a
                        :data-Name="c3.categoryName"
                        :data-level3="c3.categoryId"
                        >{{ c3.categoryName }}</a
                      >
                    </em>
                  </dd>
                </dl>
              </div>
            </div>
          </div>
        </div>
      </div>
    </transition>
  </div>
</template>

<script>
import throttle from "lodash/throttle";
import { mapState } from "vuex";
export default {
  name: "Type",
  data() {
    return {
      currentIndex: -1, //主页-控制鼠标选中
      show: true, //搜索页-控制列表显示隐藏
    };
  },
  methods: {
    //主页-鼠标进入防抖函数
    enterIndex: throttle(function (index) {
      this.currentIndex = index; //选中的列表项
    }, 50),
    //主页-鼠标离开
    leaveShow() {
      this.currentIndex = -1; //主页-鼠标离开
      if (this.$route.path != "/home") {
        this.show = false; //搜索页-鼠标离开隐藏列表
      }
    },
    //搜索页-列表路由跳转性能优化：事件委派 + 自定义标签属性
    goSearch(e) {
      let { name, level1, level2, level3 } = e.target.dataset; //获取触发的子节点信息
      //判断标签是否自定义name
      if (name) {
        let location = { name: "Search" }; //创建跳转对象
        let query = { categoryName: name }; //创建跳转对象的query参数
        if (level1) {
          query.categoryId = level1;
        } else if (level2) {
          query.categoryId = level2;
        } else {
          query.categoryId = level3;
        }
        location.query = query;
        let params = this.$route.params; //获取之前搜索遗留下来的params参数
        if (params.keyword) {
          location.params = params; //添加params参数到location对象
        }
        this.$router.push(location); //路由跳转道搜索页
      }
    },
    //搜索页-鼠标进入显示列表
    enterShow() {
      if (this.$route.path != "/home") {
        this.show = true; //搜索页-显示列表
      }
    },
  },
  computed: {
    ...mapState("home", ["categoryList"]),
  },
  mounted() {
    //搜索页-设置列表为默认隐藏false
    if (this.$route.path != "/home") {
      this.show = false;
    }
  },
};
</script>

<style scoped lang="less">
.all {
  width: 210px;
  height: 45px;
  background-color: #3498db;
  line-height: 45px;
  text-align: center;
  color: #fff;
  font-size: 14px;
  font-weight: bold;
}
.sort {
  position: absolute;
  left: 0;
  top: 45px;
  width: 210px;
  height: 461px;
  position: absolute;
  background: #fafafa;
  z-index: 999;
  .all-sort-list2 {
    height: 100%;
    overflow: auto;
    overflow-y: scroll;
    .item {
      h3 {
        line-height: 30px;
        font-size: 14px;
        font-weight: 400;
        overflow: hidden;
        padding: 0 20px;
        margin: 0;

        a {
          color: #333;
        }
      }

      .item-list {
        display: none;
        position: absolute;
        width: 734px;
        min-height: 460px;
        background: #f7f7f7;
        left: 210px;
        border: 1px solid #ddd;
        top: 0;
        z-index: 9999 !important;

        .subitem {
          float: left;
          width: 650px;
          padding: 0 4px 0 8px;

          dl {
            border-top: 1px solid #eee;
            padding: 6px 0;
            overflow: hidden;
            zoom: 1;

            &.fore {
              border-top: 0;
            }

            dt {
              float: left;
              width: 54px;
              line-height: 22px;
              text-align: right;
              padding: 3px 6px 0 0;
              font-weight: 700;
            }

            dd {
              float: left;
              width: 415px;
              padding: 3px 0 0;
              overflow: hidden;

              em {
                float: left;
                height: 14px;
                line-height: 14px;
                padding: 0 8px;
                margin-top: 5px;
                border-left: 1px solid #ccc;
              }
            }
          }
        }
      }
    }
    .item-hover {
      background-color: #73c3f8;
    }
  }
}

.sort-enter-active {
  animation: height-in 0.5s;
}
.sort-leave-active {
  animation: height-in 0.2s reverse;
}
@keyframes height-in {
  0% {
    height: 0px;
  }

  100% {
    height: 461px;
  }
}
</style>
